package com.animal.controller;


import com.animal.common.Constants;
import com.animal.common.Result;
import com.animal.entity.dto.AdoptDto;
import com.animal.entity.pojo.AAdopt;
import com.animal.entity.pojo.AComment;
import com.animal.entity.pojo.APet;
import com.animal.service.AdoptService;
import com.animal.service.CommentService;
import com.animal.service.PetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/adopt")
public class AdoptController {

    @Resource
    private AdoptService adopttService;
    @Autowired
    private PetService petService;
    @Resource
    private CommentService commentService;

    @RequestMapping("/login")
    public String login(Model model) {
        List<APet> list =petService.getNoAdopt();
        model.addAttribute("listdop", list);
        return "adopt";
    }


    @RequestMapping("getAdopAllByPetId")
    public String getAdopAllByPetId(@RequestParam(value = "petId") Integer petId, Model model) {
        APet aPet = petService.getPetById(petId);
        model.addAttribute("aPet", aPet);
        return "show";
    }

    @RequestMapping(value = "/addAdoptt",method = RequestMethod.POST)
    private String addAdoptt(AAdopt aadoptt){
        boolean flag = adopttService.addAdopttAll(aadoptt);
        if (flag) {
            return "redirect:/adopt/login";
        } else {
            return "error";
        }
    }


    @RequestMapping("/commjson")
    @ResponseBody
    public String commjson(@RequestParam("replyName") String replyName,
                           @RequestParam("content") String content,
                           @RequestParam("petId") Integer petId) {
        AComment comment = new AComment();
        comment.setAuthor(replyName);
        comment.setComment(content);
        comment.setPetId(petId);
        int count = commentService.insertComment(comment);
        if (count > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    /**
     * 查询信息
     * @param apply
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @PostMapping(value = {"/getAdopt/{pageNo}/{pageSize}",
            "/getAdopt/{apply}/{pageNo}/{pageSize}"})
    public Result getAdopt(@PathVariable(required = false) Integer apply,
                           @PathVariable Integer pageNo,
                           @PathVariable Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<AdoptDto> list = adopttService.getInfo(apply);
        PageInfo<AdoptDto> pageInfo = new PageInfo<>(list);
        if (list != null) {
            return Result.success(pageInfo);
        } else {
            return Result.error();
        }
    }

    /**
     * 审核
     * @param aAdopt
     * @return
     */
    @ResponseBody
    @PostMapping("/updateApply")
    public Result updateApply(@RequestBody AAdopt aAdopt){
        boolean b = adopttService.applyProcess(aAdopt);
        if (b) {
            return Result.success();
        }else {
            return Result.error();
        }
    }
}

